package sameClasses;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class Bytehandler {
  
  public static byte[] pluginDataToBytes(Object data) {
    byte[] plugindataAsBytes = null;
    try {
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      ObjectOutputStream oos = new ObjectOutputStream(bos);
      oos.writeObject(data);
      oos.flush();
      plugindataAsBytes = bos.toByteArray();
      oos.close();
      bos.close();
    } catch (IOException e1) {
      e1.printStackTrace();
    }
    return plugindataAsBytes;
  }
  
  public static Object BytesToPlugindata (byte[] data) {
    ByteArrayInputStream in = new ByteArrayInputStream(data);
    try {
      ObjectInputStream is = new ObjectInputStream(in);
      Object ret = is.readObject();
      is.close();
      return ret;
    } catch (IOException e) {
      e.printStackTrace();
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    }
    return data;
  }
}
